

active_sites=[1 2 4 7 8];
passive_sites=[3 5 6 9];
control_sites=[10];



l_win=7;
r_win=50;

t_point = 1;

hf=figure;hold on
ps_resp=[];ps_snip_all=[];
for site_id=1:9
    curr_roi=[];
    curr_layer=[];
    for ind=1:4
        cells_per_layer(ind)=size(proj_meta(site_id).rd(ind,t_point).act,1);
        curr_roi=[curr_roi 1:cells_per_layer(ind)];
        curr_layer=[curr_layer ind*ones(1,cells_per_layer(ind))];
    end
    
    for roi_id=1:sum(cells_per_layer)
        for t_point=1:4
%             figure(hf)
            cell_act=proj_meta(site_id).rd(curr_layer(roi_id),t_point).act(curr_roi(roi_id),:);
%             plot(0.5*proj_meta(site_id).rd(layer,t_point).ps_id.*(abs(proj_meta(site_id).rd(layer,t_point).velM_smoothed)>0.005)+7-2*t_point,'r');
%             plot(cell_act+7-2*t_point);
%             axis tight
            pb_onsets=find(diff(( proj_meta(site_id).rd(1,t_point).ps_id.*(abs(proj_meta(site_id).rd(1,t_point).velM_smoothed)<0.001).*(abs(proj_meta(site_id).rd(1,t_point).velP_smoothed)>0.001) )>0)==1);
            ps_onsets=find(diff(( proj_meta(site_id).rd(1,t_point).ps_id.*(abs(proj_meta(site_id).rd(1,t_point).velM_smoothed)>0.001) )>0)==1);
            
            fract_running_during_pb(site_id,t_point)=sum(abs(proj_meta(site_id).rd(1,t_point).velM_smoothed(7501:end))>0.001)/(size(proj_meta(site_id).rd(1,t_point).velM_smoothed,2)-7500);
            fract_running_during_fb(site_id,t_point)=sum(abs(proj_meta(site_id).rd(1,t_point).velM_smoothed(1:7500))>0.001)/7500;
            
            ps_onsets(ps_onsets>22000)=[];
            ps_onsets(ps_onsets<7)=[];
            ps_onsets(find(diff(ps_onsets)<20)+1)=[];
            
            pb_onsets(pb_onsets>22000)=[];
            pb_onsets(pb_onsets<7)=[];
            pb_onsets(find(diff(pb_onsets)<20)+1)=[];
            
            nbr_ps(site_id,t_point)=length(ps_onsets);
            ps_snips=[];
            if length(ps_onsets)>0
                for ind=1:length(ps_onsets)
                    ps_snips(:,ind)=cell_act(ps_onsets(ind)-l_win:ps_onsets(ind)+r_win);
                    ps_snip_all(site_id,roi_id,t_point,ind,:)=cell_act(ps_onsets(ind)-l_win:ps_onsets(ind)+r_win);
                end
                tmp=mean(ps_snips');
                ps_resp(site_id,roi_id,t_point)=mean(tmp(10:30))-mean(tmp(1:7));
            end
            
            nbr_pp(site_id,t_point)=length(pb_onsets);
            pb_snips=[];
            if length(pb_onsets)>0
                for ind=1:length(pb_onsets)
                    pb_snips(:,ind)=cell_act(pb_onsets(ind)-l_win:pb_onsets(ind)+r_win);
                    pb_snip_all(site_id,roi_id,t_point,ind,:)=cell_act(pb_onsets(ind)-l_win:pb_onsets(ind)+r_win);
                end
                tmp=mean(pb_snips');
                pb_resp(site_id,roi_id,t_point)=mean(tmp(10:30))-mean(tmp(1:7));
            end
            
        end
    end
end

ps_resp(ps_resp==0)=nan;
ps_snip_all(ps_snip_all==0)=nan;

pb_resp(pb_resp==0)=nan;
pb_snip_all(pb_snip_all==0)=nan;



ps_act=squeeze(nanmean(nanmean(nanmean(nanmean(ps_snip_all(active_sites,:,:,1:5,:),1),2),3),4));
ps_pas=squeeze(nanmean(nanmean(nanmean(nanmean(ps_snip_all(passive_sites,:,:,1:5,:),1),2),3),4));

pb_act=squeeze(nanmean(nanmean(nanmean(nanmean(pb_snip_all(active_sites,:,:,1:5,:),1),2),3),4));
pb_pas=squeeze(nanmean(nanmean(nanmean(nanmean(pb_snip_all(passive_sites,:,:,1:5,:),1),2),3),4));

figure;hold on
plot(ps_act-mean(ps_act(1:7)),'b')
plot(ps_pas-mean(ps_pas(1:7)),'r')
plot(pb_act-mean(pb_act(1:7)),':');
plot(pb_pas-mean(pb_pas(1:7)),'r:');


figure;
hold on;
for ind=1:1
    
    plot(squeeze(nanmean(nanmean(ps_snip_all(3,:,:,ind,:),3),2))')
end












